using System;
using System.Globalization;
using EnvDTE;
using EnvDTE80;
using Microsoft.Practices.RecipeFramework;
using PrismFactory.SoftwareFactory.Properties;

namespace PrismFactory.SoftwareFactory.Extensions.Actions.VisualStudio
{
    /// <summary>
    /// Action that compiles the solution
    /// </summary>
    public class CompileSolutionAction : ConfigurableAction
    {
        #region Input Properties

        private bool throwException;

        /// <summary>
        /// Gets or sets a value indicating whether [throw exception].
        /// </summary>
        /// <value><c>true</c> if [throw exception]; otherwise, <c>false</c>.</value>
        [Input(Required = false)]
        public bool ThrowException
        {
            get { return throwException; }
            set { throwException = value; }
        }

        #endregion

        #region ConfigurableAction Implementation

        /// <summary>
        /// See <see cref="M:Microsoft.Practices.RecipeFramework.IAction.Execute"/>.
        /// </summary>
        public override void Execute()
        {
            var vs = (DTE) GetService(typeof (DTE));

            vs.Solution.SolutionBuild.Build(true);

            if (vs.Solution.SolutionBuild.LastBuildInfo != 0)
            {
                ((DTE2) vs).ToolWindows.ErrorList.Parent.Activate();

                if (throwException)
                {
                    throw new ActionExecutionException(String.Format(CultureInfo.CurrentCulture, Resources.CompilationFailed, vs.Solution.SolutionBuild.LastBuildInfo));
                }
            }
        }

        /// <summary>
        /// See <see cref="M:Microsoft.Practices.RecipeFramework.IAction.Undo"/>.
        /// </summary>
        public override void Undo()
        {
            //Not Implemented
        }

        #endregion
    }
}